package Offer.offer;

import java.util.HashMap;
import java.util.Map;

/**
 * 字符数组中第一个只出现一次的字符
 * Created by 雷文康 on 2018/1/4.
 */
public class Test35字符数组中第一个只出现一次的字符 {
    public static void main(String[] args) {
        String str = "abbacdc";
        System.out.println(findOnceNum(str.toCharArray()));

    }

    public static char findOnceNum(char[] ch) {
        if (ch == null || ch.length == 0) {
            System.out.println("输入非法！");
            return ' ';
        }
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < ch.length; i++) {
            if (!map.containsKey(ch[i])) {
                map.put(ch[i], 1);
            } else {
                map.put(ch[i], map.get(ch[i]) + 1);
            }
        }

        for (int i = 0; i < ch.length; i++) {
            if (map.get(ch[i]) == 1){
                return ch[i];
            }
        }
        return ' ';

    }
}
